{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6920\n",
      "872\n",
      "1821\n"
     ]
    }
   ],
   "source": [
    "import nltk\n",
    "a = nltk.corpus.BracketParseCorpusReader(\".\", \"(train|dev|test)\\.txt\")\n",
    "\n",
    "text = {}\n",
    "labels = {}\n",
    "keys = ['train', 'dev', 'test']\n",
    "for k in keys :\n",
    "    text[k] = [x.leaves() for x in a.parsed_sents(k+'.txt') if x.label() != '2']\n",
    "    labels[k] = [int(x.label()) for x in a.parsed_sents(k+'.txt') if x.label() != '2']\n",
    "    print(len(text[k]))\n",
    "    \n",
    "import spacy\n",
    "nlp = spacy.load('en', disable=['parser', 'tagger', 'ner'])\n",
    "import re\n",
    "\n",
    "def tokenize(text) :\n",
    "    text = \" \".join(text)\n",
    "    text = text.replace(\"-LRB-\", '')\n",
    "    text = text.replace(\"-RRB-\", \" \")\n",
    "    text = re.sub(r'\\W', ' ', text)\n",
    "    text = re.sub(r'\\s+', ' ', text)\n",
    "    text = text.strip()\n",
    "    tokens = \" \".join([t.text.lower() for t in nlp(text)])\n",
    "    return tokens\n",
    "\n",
    "for k in keys :\n",
    "    text[k] = [tokenize(t) for t in text[k]]\n",
    "    labels[k] = [1 if x >= 3 else 0 for x in labels[k]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df_texts = []\n",
    "df_labels = []\n",
    "df_exp_split = []\n",
    "\n",
    "for k in keys :\n",
    "    df_texts += text[k]\n",
    "    df_labels += labels[k]\n",
    "    df_exp_split += [k]*len(text[k])\n",
    "    \n",
    "df = pd.DataFrame({'text' : df_texts, 'label' : df_labels, 'exp_split' : df_exp_split}) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('sst_dataset.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vocabulary size :  13826\n",
      "Found 11171 words in model out of 13826\n"
     ]
    }
   ],
   "source": [
    "%run \"../preprocess_data_BC.py\" --data_file sst_dataset.csv --output_file ./vec_sst.p --word_vectors_type fasttext.simple.300d --min_df 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
